package com.example.software.controller;

import com.example.software.entity.User;
import com.example.software.service.TaskService;
import com.example.software.service.UserService;
import com.example.software.vo.Result;
import com.example.software.vo.TaskVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/admin")
public class AdminController {
    
    private static final Logger log = LoggerFactory.getLogger(AdminController.class);
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private TaskService taskService;
    
    /**
     * 获取所有用户列表
     */
    @GetMapping("/users")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<List<User>> getAllUsers() {
        log.info("管理员获取所有用户列表");
        List<User> users = userService.getAllUsers();
        // 处理用户列表，避免返回密码
        users.forEach(user -> user.setPassword(null));
        return Result.success(users);
    }
    
    /**
     * 删除用户
     */
    @DeleteMapping("/users/{userId}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Boolean> deleteUser(@PathVariable Long userId) {
        log.info("管理员删除用户，用户ID: {}", userId);
        boolean result = userService.deleteUser(userId);
        return Result.success(result);
    }
    
    /**
     * 获取所有任务列表
     */
    @GetMapping("/tasks")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<List<TaskVO>> getAllTasks() {
        log.info("管理员获取所有任务列表");
        List<TaskVO> tasks = taskService.getTaskList(null);  // 传入null表示获取所有任务
        return Result.success(tasks);
    }
    
    /**
     * 删除任务
     */
    @DeleteMapping("/tasks/{taskId}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Boolean> deleteTask(@PathVariable Long taskId) {
        log.info("管理员删除任务，任务ID: {}", taskId);
        boolean result = taskService.adminDeleteTask(taskId);
        return Result.success(result);
    }
    
    /**
     * 上传分析数据
     */
    @PostMapping("/upload-analytics")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Map<String, Object>> uploadAnalyticsData(@RequestParam("file") MultipartFile file) {
        log.info("管理员上传分析数据，文件名: {}", file.getOriginalFilename());
        try {
            Map<String, Object> result = taskService.processAnalyticsData(file);
            return Result.success(result);
        } catch (Exception e) {
            log.error("上传分析数据失败", e);
            return Result.error("上传分析数据失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取统计数据
     */
    @GetMapping("/statistics")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Map<String, Object>> getStatistics() {
        log.info("管理员获取平台统计数据");
        Map<String, Object> statistics = taskService.getAdminStatistics();
        return Result.success(statistics);
    }
} 